package ml.iray.bean;

/**
 * Created by boot on 12/13/15.
 * <p>
 * 实现了ICode接口.
 * 使用数据库内部ID和Code编码的实体类的基类（抽象类）
 * 实现了自身属性的Equals(object obj)和GetHashCode()方法
 */
public class AbstractCode implements ICode {
    // 用以人工识别的编号
    private String code;

    @Override
    public String getCode() {
        return code;
    }

    @Override
    public void setCode(String code) {
        if (null != code && code.length() < BeanConstant.CODE_MAX_LENGTH)
            this.code = code;
        else
            throw new IllegalArgumentException("Invalid value for Code, the length of the code too long.");
    }

    @Override
    public int hashCode() {
        int hash = super.hashCode();
        hash = hash * 31 + (code == null ? 0 : code.hashCode());
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof AbstractCode) {
            AbstractCode ac = (AbstractCode) obj;
            return super.equals(ac) && code.equals(ac.getCode());
        }
        return false;
    }
}
